<?php
/**
 * Created by PhpStorm.
 * User: Sergey
 * Date: 17.03.14
 * Time: 14:07
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

define("BASE_DQ_PLUGIN_PATH", plugin_dir_path(__FILE__));

require_once BASE_DQ_PLUGIN_PATH . "classes/QuestionDao.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/AnswerDao.php";

$questionDao = new QuestionDao();
$answerDao = new AnswerDao();

if (isset($_REQUEST["quizId"])
    && isset($_REQUEST["content"])
    && isset($_REQUEST["answers"]) && is_array($_REQUEST["answers"])
) {
    $question = new Question();
    $question->id = intval($_REQUEST["id"]);
    $question->content = strval($_REQUEST["content"]);
    $question->quizId = intval($_REQUEST["quizId"]);

    if ($question->id > 0) {
        $questionDao->edit($question);
    } else {
        $question->id = $questionDao->add($question);
    }

    foreach ($_REQUEST["answers"] as $arrAnswer) {
        $answer = new Answer();
        $answer->id = intval($arrAnswer["id"]);
        $answer->content = $arrAnswer["content"];
        $answer->isCorrect =
            (isset($arrAnswer["correct"]) && $arrAnswer["correct"] == "on") ? 1 : 0;
        $answer->questionId = $question->id;

        if ($answer->id > 0) {
            $answerDao->edit($answer);
        } else {
            $answerDao->add($answer);
        }
    }
}

if (
    (
        isset($_REQUEST["quizId"])
        && isset($_REQUEST["content"])
        && isset($_REQUEST["answers"]) && is_array($_REQUEST["answers"])
    )
    || !isset($_GET["quizId"])
) {
    header(
        "Location: " . menu_page_url("distant-quiz/quiz-form.php", false) .
        "&id=" . $_REQUEST["quizId"]
    );
}

$question = null;
if (isset($_GET["id"])) {
    $question = $questionDao->getById($_GET["id"]);
    $question->answers = $answerDao->getAllByQuestionId($question->id);
}
?>

<? include_once BASE_DQ_PLUGIN_PATH . "header.php"; ?>

<h2>
    <? if (isset($_GET["id"])) { ?>
        Редактирование вопроса
    <? } else { ?>
        Добавление вопроса
    <? } ?>
</h2>

<form id="question-form" method="post">
    <table>
        <tr>
            <td></td>
            <td>
                <input type="button" class="create-answer" value="+"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="hidden" name="quizId" value="<?= intval($_GET["quizId"]) ?>"/>
                <input type="hidden" name="id" value="<?= ($question) ? $question->id : 0 ?>"/>
                <textarea
                    name="content"
                    style="width: 420px; height: 160px;"><?=
                    ($question) ? html_entity_decode($question->content) : "" ?></textarea>
            </td>
            <td>
                <ul class="answers-list">
                    <? if ($question && count($question->answers) > 0) { ?>
                        <? foreach ($question->answers as $answer) { ?>
                            <li>
                                <input type="hidden" value="<?= $answer->id ?>"/>
                                <input type="checkbox" <?= ($answer->isCorrect) ? "checked" : "" ?>/>
                                <input type="text" value="<?= $answer->content ?>"/>
                                <input type="button" class="remove-answer" value="X"/>
                            </li>
                        <? } ?>
                    <? } else { ?>
                        <li>
                            <input type="hidden" value="0"/>
                            <input type="checkbox"/>
                            <input type="text"/>
                            <input type="button" class="remove-answer" value="X"/>
                        </li>
                        <li>
                            <input type="hidden" value="0"/>
                            <input type="checkbox"/>
                            <input type="text"/>
                            <input type="button" class="remove-answer" value="X"/>
                        </li>
                        <li>
                            <input type="hidden" value="0"/>
                            <input type="checkbox"/>
                            <input type="text"/>
                            <input type="button" class="remove-answer" value="X"/>
                        </li>
                        <li>
                            <input type="hidden" value="0"/>
                            <input type="checkbox"/>
                            <input type="text"/>
                            <input type="button" class="remove-answer" value="X"/>
                        </li>
                    <? } ?>
                </ul>
            </td>
        </tr>
    </table>
    <input type="submit" value="Сохранить изменения"/>
</form>